package org.campuscontent.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import org.campuscontent.config.languages.LanguageListener;
import org.campuscontent.config.languages.LanguageManager;

public class ComboRenderer extends JLabel implements ListCellRenderer, LanguageListener
{
    private String[] itemText;
    private int[] itemHeight;

    public ComboRenderer()
    {
        setOpaque( true );
        setHorizontalAlignment( LEFT );
        setVerticalAlignment  ( TOP  );

        itemText   = new String[ ProblemCtrl.MAX_TEMPLATES ];
        itemHeight = new int   [ ProblemCtrl.MAX_TEMPLATES ];

        for ( int i = 0; i < itemText.length; i++ )
        {
            try
            {
                String txt = LanguageManager.str( this, "option." + i + ".content" );
                String hg  = LanguageManager.str( this, "option." + i + ".height"  );
                
                itemText  [ i ] =  txt;
                itemHeight[ i ] = Integer.parseInt( hg );
            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

    public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus )
    {
        // Get the selected index. (The index param isn't
        // always valid, so just use the value.)
        int selectedIndex = ((Integer) value).intValue();

        if ( isSelected )
        {
            setBackground( list.getSelectionBackground() );
            setForeground( list.getSelectionForeground() );
        }
        else
        {
            setBackground( list.getBackground() );
            setForeground( list.getForeground() );
        }

        setPreferredSize( new Dimension( 300, itemHeight[ selectedIndex ] ) );

        setText( itemText[ selectedIndex ] );

        return this;
    }

    public String getItemAt( int index )
    {
        if ( index == 4 ) return "";
        String content = itemText[ index ];
        int begin = content.lastIndexOf( "<" );
        int end = content.lastIndexOf( ">" );
        while ( begin != -1 )
        {
            content = content.substring( 0, begin ) + content.substring( end + 1, content.length() );
            begin = content.lastIndexOf( "<" );
            end = content.lastIndexOf( ">" );
        }
        // content = content.replaceAll("\n", "");
        // content = content.replaceAll(" ", "");
        content = content.substring( 9 );
        // try {
        // byte[] temp = content.getBytes("ISO-8859-1");
        // content = new String(temp);
        // } catch (UnsupportedEncodingException e) {
        // e.printStackTrace();
        // }
        if ( index < 10 ) return content;
        else
        {
            content = content.substring( 1, content.length() );
            return content;

        }
    }
    
    public void update( ResourceBundle resources )
    {
        for ( int i = 0; i < itemText.length; i++ )
        {
            try
            {
                itemText  [ i ] = LanguageManager.str( this, "option." + i + ".content" ); 
                itemHeight[ i ] = Integer.parseInt( LanguageManager.str( this, "option." + i + ".height" ) );
            }
            catch ( Exception e )
            {
                System.out.println( "ComboRenderer::update :  " + i );
                e.printStackTrace();
            }
        }
    }
}
